<?php
namespace app\index\controller\Project;

use app\index\controller\Base;

class contract extends base_project
{
    public $tableName = "ProjectContract";
    public function index()
    {
        $type=input("type");
        $search=input("search");
        $where=[];
        if(!empty($type)){
            $where["mtype"]=$type;
        }
        if(!empty($search)){
            $where["file_name"]=array("like","%".trim($search)."%");
        }
        $this->page($where);
    

        $file_type= model("CodeMap")->get_value("contract_type"); 
        $contract_typea= model("CodeMap")->get_value("contract_typea"); 
       
        $data["type"]= $type; 
        $data["file_type"] = $file_type;
        $data["file_type_item"] = array_column($file_type, "m_name", "m_value");
        $data["contract_typea_item"] = array_column($contract_typea, "m_name", "m_value");
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
    
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();         
         
            $files= model("File")->where(["id"=>array("in",$post["file_id"])])->select();
         
            $file_data=[];
            foreach($files as $t_file){
             $data["project_id"] = $post["project_id"];
             $data["mtype"] = $post["mtype"];
             $data["save_name"] = $t_file["save_name"];      
             $data["uid"] =$this->member_id;
             $data["mtypea"] =$post["mtypea"];
             
             $data["file_name"] = $t_file["file_name"];
             $data["file_path"] = $t_file["save_path"];
             $file_data[]=$data;
            }
            $result=0;
            $ProjectContractModel = model("ProjectContract");
            if (!empty($file_data)) {
                $result = $ProjectContractModel->saveAll($file_data);
            }
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Contract/index"));
        }
       $data["file_type"]= model("CodeMap")->get_value("contract_type"); 
       $data["contract_typea"]= model("CodeMap")->get_value("contract_typea");
       $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectContractModel = model('ProjectContract');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["id"] = $post["id"];
            $data["project_id"] = $post["project_id"];
            $data["mtype"] = $post["mtype"];
            $data["file_name"] = $post["file_name"];
            $data["mtypea"] =$post["mtypea"];
            $data["uid"] = $post["uid"];
            $data["create_time"] = $post["create_time"];
            $data["update_time"] = $post["update_time"];
            $data["delete_time"] = $post["delete_time"];

            $map["id"] = $post["id"];
            $result = $this->operateData($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Contract/index"));
        }

        $id = input("id");
        $data["file_type"]= model("CodeMap")->get_value("contract_type"); 
        $data["info"] = $ProjectContractModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $result = $this->delBase(["id" => intval($id)]);
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
